package zr;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import zr.component.PlayerInfoPane;
import zr.dao.Operation;
import zr.dao.Player;
import zr.dao.QiPan;
import zr.dao.QiZi;
import zr.dao.QiZiName;
import zr.util.Logger;

public class Main {

	static List<QiZi> reds = QiZi.generateAll(Color.red);
	static List<QiZi> yellows = QiZi.generateAll(Color.yellow);
	public static List<QiZi> all = new ArrayList<QiZi>();

	private final static org.apache.log4j.Logger logger = Logger
			.getInstance(Main.class);
	public final static int SERVER_PORT = 7947;
	private final static int TIME_FOR_THINKING = 120;
	public static QiPan qp = new QiPan();
	public static QiZi currentSelectQizi = null;
	static ServerSocket server;
	public static JFrame frame = new JFrame();
	static JPanel playerInfo = new JPanel();
	public static Player me;
	public static Player he;
	static PlayerInfoPane myInfo, hisInfo;
	public static boolean myTurn = true;
	public static DataInputStream input;
	public static DataOutputStream output;

	static String room = "";
	
	static JButton getPath=new JButton("显示一步可达位置");
	static JButton delete=new JButton("删除");

	public static void server() {
		Thread t = new Thread() {
			public void run() {
				ServerSocket server;
				try {
					server = new ServerSocket(SERVER_PORT);
					Socket client = server.accept();

					input = new DataInputStream(client.getInputStream());
					output = new DataOutputStream(client.getOutputStream());

					String name = input.readUTF();
					String ip = input.readUTF();
					he = new Player(name);
					he.setIp(ip);
					addHisInfoPane();
					output.writeUTF(room);
					output.writeUTF(me.getName());
					output.writeUTF(me.getIp());

					output.writeUTF(all.toString());

					startClockThread();

					waitCommand();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		};
		t.start();
	}

	public static void startClockThread() {
		Thread clock = new Thread() {
			public void run() {
				while (true) {
					if (myTurn) {
						if (me.leftTime == Player.FU1) {// 我的计时开始
							me.leftTime = TIME_FOR_THINKING;
							he.leftTime = Player.FU1;
						}
						me.leftTime--;
						if (me.leftTime == 0)
							myTurn = false;

					} else {
						if (he.leftTime == Player.FU1) {// 对方的计时开始
							he.leftTime = TIME_FOR_THINKING;
							me.leftTime = Player.FU1;
						}
						he.leftTime--;
						if (he.leftTime == 0)
							myTurn = true;
					}

					myInfo.setLeftTime(me.getLeftTime());
					hisInfo.setLeftTime(he.getLeftTime());
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		clock.start();
	}

	public static void waitCommand() throws IOException {
		while (true) {
			String command = input.readUTF();
			logger.info("收到指令" + command);
			String s[] = command.split(":");
			String type = s[0];
			log(he+": "+command);
			if (type.equals(Operation.定色.toString())) {
				String point = s[1].substring(1, s[1].length() - 1);
				String ss[] = point.split(",");
				int row = Integer.parseInt(ss[0]);
				int col = Integer.parseInt(ss[1]);
				QiZi qz = QiZi.get(row, col);
				Main.setColor(getOppositeColor(qz.color));
			} else if (type.equals(Operation.翻子.toString())) {
				String point = s[1].substring(1, s[1].length() - 1);
				String ss[] = point.split(",");
				int row = Integer.parseInt(ss[0]);
				int col = Integer.parseInt(ss[1]);
				QiZi qz = QiZi.get(row, col);
				qz.changeStatus(false);
			} else if (type.equals(Operation.吃子.toString())) {
				String point = s[1].substring(1, s[1].length() - 1);
				String ss[] = point.split("\\]\\[");
				String ss0[] = ss[0].split(",");
				String ss1[] = ss[1].split(",");
				int x1 = Integer.parseInt(ss0[0]);
				int y1 = Integer.parseInt(ss0[1]);
				int x2 = Integer.parseInt(ss1[0]);
				int y2 = Integer.parseInt(ss1[1]);
				QiZi qz1 = QiZi.get(x1, y1);
				QiZi qz2 = QiZi.get(x2, y2);
				qz1.moveTo(new Point(x2, y2));
				qz2.destroy();
			}else if (type.equals(Operation.并子.toString())) {
				String point = s[1].substring(1, s[1].length() - 1);
				String ss[] = point.split("\\]\\[");
				String ss0[] = ss[0].split(",");
				String ss1[] = ss[1].split(",");
				int x1 = Integer.parseInt(ss0[0]);
				int y1 = Integer.parseInt(ss0[1]);
				int x2 = Integer.parseInt(ss1[0]);
				int y2 = Integer.parseInt(ss1[1]);
				QiZi qz1 = QiZi.get(x1, y1);
				QiZi qz2 = QiZi.get(x2, y2);
				qz1.destroy();
				qz2.destroy();
			}else if (type.equals(Operation.走子.toString())) {
				String point = s[1].substring(1, s[1].length() - 1);
				String ss[] = point.split("\\]\\[");
				String ss0[] = ss[0].split(",");
				String ss1[] = ss[1].split(",");
				int x1 = Integer.parseInt(ss0[0]);
				int y1 = Integer.parseInt(ss0[1]);
				int x2 = Integer.parseInt(ss1[0]);
				int y2 = Integer.parseInt(ss1[1]);
				QiZi qz = QiZi.get(x1, y1);
				qz.moveTo(new Point(x2,y2));
			}else if(type.equals(Operation.胜.toString())){
				JOptionPane.showMessageDialog(Main.frame, "你输了！");
			}
				
			Main.myTurn = true;
			Main.currentSelectQizi = null;
			qp.updateUI();
		}

	}

	public static void start() {

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int width = 900, height = 700;
		frame.setSize(width, height);
		frame.setLayout(null);
		qp.setLocation((width - qp.getWidth()) / 2,
				(height - qp.getHeight()) / 2 - 20);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((int) (screenSize.getWidth() - width) / 2,
				(int) (screenSize.getHeight() - height) / 2);
		frame.add(qp);
		frame.setResizable(false);

		playerInfo.setLayout(new BorderLayout());
		playerInfo
				.setSize((int) (qp.getLocation().getX() - 20), qp.getHeight());
		playerInfo.setLocation(10, (int) qp.getLocation().getY());
		System.out.println(playerInfo.getLocation());
		System.out.println(playerInfo.getSize());
		frame.add(playerInfo);
		frame.setVisible(true);
		if (me == null)
			login();
		addMyInfoPane();
		
		addLogPane();

//		getPath.setLocation((int) qp.getBounds().getMaxX(),qp.getY());
//		getPath.setSize(100, 25);
//		delete.setLocation((int) qp.getBounds().getMaxX(),qp.getY()+30);
//		delete.setSize(100, 25);
//		frame.add(getPath);
//		frame.add(delete);
//		getPath.addActionListener(new ActionListener(){
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				for(JLabel l:kedas){
//					qp.remove(l);
//				}
//				kedas.clear();
//				if(Main.currentSelectQizi!=null){
//					List<Point> ps=Operation.oneStepArea(Main.currentSelectQizi);
//					for(Point p:ps){
//						System.out.println("可达点："+p);
//						JLabel label=new JLabel("可达");
//						label.setBackground(Color.pink);
//						label.setSize(50,30);
//						label.setLocation(qp.qipan[p.x][p.y][0],qp.qipan[p.x][p.y][1]);
//						kedas.add(label);
//						qp.add(label);
//						label.updateUI();
//					}
//				}
//			}			
//		});
//		delete.addActionListener(new ActionListener(){
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				if(Main.currentSelectQizi!=null){
//					Main.currentSelectQizi.destroy();
//				}
//			}
//			
//		});
	}
	static JTextArea log=new JTextArea();
	static JScrollPane scroll=new JScrollPane(log);
	private static void addLogPane() {
		scroll.setSize(playerInfo.getWidth(), qp.getHeight());
		scroll.setLocation((int) (qp.getBounds().getMaxX()+10), qp.getY());
		frame.add(scroll);
	}
	public static void log(String line){
		log.append(line+"\n");
		log.updateUI();
	}
	static List<JLabel> kedas=new ArrayList<JLabel>();
	private static void login() {
		while (room == null || room.length() == 0) {
			room = JOptionPane.showInputDialog(frame, "请输入您的房间名称", "房间名称",
					JOptionPane.QUESTION_MESSAGE);
			if (room != null)
				room = room.trim();
		}
		frame.setTitle(room);
		logger.info("房间名称：" + room);
		String userid = "";
		while (userid == null || userid.length() == 0) {
			userid = JOptionPane.showInputDialog(frame, "请输入您的名字", "玩家名字",
					JOptionPane.QUESTION_MESSAGE);
			if (userid != null)
				userid = userid.trim();
		}
		me = new Player(userid);

		all.addAll(reds);
		all.addAll(yellows);
		qp.randomLayoutQiZi(all);
	}

	private static void addMyInfoPane() {
		myInfo = new PlayerInfoPane(me);
		playerInfo.add(myInfo, BorderLayout.SOUTH);
		playerInfo.updateUI();
	}

	public static void addHisInfoPane() {
		hisInfo = new PlayerInfoPane(he);
		playerInfo.add(hisInfo, BorderLayout.NORTH);
		playerInfo.updateUI();
	}

	public static void main(String[] args) {
		layout("[[工兵，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,17,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=0,y=0]], [炸弹，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,109,17,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=0,y=1]], [地雷，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,203,17,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=0,y=2]], [地雷，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,297,17,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=0,y=3]], [司令，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,17,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=0,y=4]], [连长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,59,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=1,y=0]], [连长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,109,59,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=1,y=1]], [连长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,203,59,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=1,y=2]], [团长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,297,59,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=1,y=3]], [地雷，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,387,59,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=1,y=4]], [排长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,19,100,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=2,y=0]], [团长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,100,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=2,y=2]], [排长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,100,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=2,y=4]], [旅长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,19,143,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=3,y=0]], [地雷，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,109,143,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=3,y=1]], [军旗，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,297,143,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=3,y=3]], [师长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,387,143,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=3,y=4]], [炸弹，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,186,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=4,y=0]], [旅长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,203,186,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=4,y=2]], [旅长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,387,186,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=4,y=4]], [团长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,230,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=5,y=0]], [工兵，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,109,230,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=5,y=1]], [工兵，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,203,230,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=5,y=2]], [排长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,297,230,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=5,y=3]], [排长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,230,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=5,y=4]], [军长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,358,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=6,y=0]], [司令，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,109,358,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=6,y=1]], [地雷，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,358,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=6,y=2]], [炸弹，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,297,358,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=6,y=3]], [工兵，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,387,358,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=6,y=4]], [工兵，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,19,400,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=7,y=0]], [团长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,400,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=7,y=2]], [炸弹，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,387,400,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=7,y=4]], [连长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,19,445,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=8,y=0]], [工兵，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,109,445,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=8,y=1]], [师长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,297,445,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=8,y=3]], [营长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,445,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=8,y=4]], [旅长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,489,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=9,y=0]], [营长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,489,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=9,y=2]], [师长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,489,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=9,y=4]], [营长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,19,526,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=10,y=0]], [师长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,109,526,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=10,y=1]], [连长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,526,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=10,y=2]], [排长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,297,526,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=10,y=3]], [营长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,526,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=10,y=4]], [军旗，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,19,566,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=11,y=0]], [地雷，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,109,566,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=11,y=1]], [军长，java.awt.Color[r=255,g=0,b=0]，javax.swing.JButton[,203,566,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=11,y=2]], [排长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,297,566,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=11,y=3]], [连长，java.awt.Color[r=255,g=255,b=0]，javax.swing.JButton[,387,566,50x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1549ceb,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=java.awt.Insets[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]，java.awt.Point[x=11,y=4]]]");
	}

	public static void layout(String buju) {
		buju = buju.substring(1, buju.length() - 1);
		String qzs[] = buju.split(", ");
		for (String qz : qzs) {
			qz = qz.substring(1, qz.length() - 1);
			String pros[] = qz.split("，");
			String name = pros[0];
			String colorStr = pros[1];
			int r = Integer.parseInt(colorStr.substring(
					colorStr.indexOf("r=") + 2, colorStr.indexOf(",g=")));
			int g = Integer.parseInt(colorStr.substring(
					colorStr.indexOf("g=") + 2, colorStr.indexOf(",b=")));
			int b = Integer.parseInt(colorStr.substring(
					colorStr.indexOf("b=") + 2, colorStr.length() - 1));
			Color color = new Color(r, g, b);
			String button = pros[2];
			button = button.substring(button.indexOf("javax.swing.JButton[,")
					+ "javax.swing.JButton[,".length());
			int x = Integer.parseInt(button.substring(0, button.indexOf(",")));
			button = button.substring(button.indexOf(",") + 1);
			int y = Integer.parseInt(button.substring(0, button.indexOf(",")));
			String pointStr = pros[3];
			int row = Integer.parseInt(pointStr.substring(pointStr
					.indexOf("x=") + 2, pointStr.indexOf(",y")));
			int col = Integer.parseInt(pointStr.substring(pointStr
					.indexOf("y=") + 2, pointStr.length() - 1));
			Point point = new Point(row, col);
			QiZi qizi = new QiZi(QiZiName.instance(name), color);
			qizi.button.setLocation(x, y);
			qizi.point = point;
			all.add(qizi);
			qp.add(qizi.button);
		}
	}

	public static void setColor(Color color) {
		me.color = color;
		myInfo.setColor(color);
		Color other = getOppositeColor(color);
		he.color = other;
		hisInfo.setColor(other);
	}

	public static void write(String command) {
		try {
			output.writeUTF(command);
			logger.info("发送指令" + command);
			log(me+": "+command);
			Main.myTurn = false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Color getOppositeColor(Color color) {
		return color.equals(Color.red) ? Color.yellow : Color.red;
	}
}
